enum BUTTONS {
    //% block="A"
    A2,
    //% block="B"
    A3,
    //% block="C"
    A4,
    //% block="D"
    A5,
    //% block="停止"
    A1,
    //% block="E"
    A6,
    //% block="F"
    A7,

    //% block="中"
    D0,
    //% block="左"
    D1,
    //% block="右"
    D2,
    //% block="下"
    D3,
    //% block="上"
    D4,
    //% block="左下"
    D5,
    //% block="右下"
    D6,
    //% block="左上"
    D7,
    //% block="右上"
    D8,
}


//% color="#2494F4" iconWidth=50 iconHeight=40
namespace MicroblueY3Control{

    
    //% block="Y3蓝牙手柄: MAC地址=[MAC_ADDR]" blockType="command" 
    //% MAC_ADDR.shadow="string"    MAC_ADDR.defl="11:52:A0:8F:0F:46"

    export function control_init(parameter: any, block: any) {
        let mac_addr = parameter.MAC_ADDR.code;

        Generator.addImport("from mb_y3_control import Y3Control");
        Generator.addInitHeader("y3", `y3 = Y3Control(${mac_addr})`, true)
    }

  
    //% block="当Y3蓝牙手柄被操控时" blockType="hat"

    export function control_receive(parameter: any, block: any) {
        Generator.addInitHeader("y3_callback", `y3.connect(y3_control_callback)`, true)
        Generator.addEvent("Y3ControlReceive","y3_control_callback",`y3_value`);
    }


    //% block="Y3蓝牙手柄按键值" blockType="reporter"

    export function y3_value(parameter: any, block: any) {
        Generator.addCode(`y3_value`);
    }

    //% block="Y3蓝牙手柄按键[BTN]被按下？" blockType="boolean"
    //% BTN.shadow="dropdownRound"   BTN.options="BUTTONS"     BTN.defl="BUTTONS.tm1"

    export function y3_is_press(parameter: any, block: any) {
        let btn = parameter.BTN.code; 
        Generator.addCode(`y3_value == "${btn}"`);
    }
    
    
}
